package com.david.jetpack.room

import androidx.room.Dao
import androidx.room.Entity
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.PrimaryKey
import androidx.room.Query
import kotlinx.coroutines.flow.Flow

/**
 * 场景：将地图数据下载至本地存储到sqlite中，以提高加载速度，
 * 根据地图聚合级别添加多个数据库，如5m-100m时从一个数据库获取数据，100m-1000m时从另一个数据库获取聚合点数据
 */
@Dao
interface AddressDao {
    @Query("select * from address")
    fun getAll(): Flow<List<AddressInfo>>

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun insertAddr(addressInfo: AddressInfo)

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun insertAllAddr(addressInfos: List<AddressInfo>)

    @Query("delete from address")
    fun deleteAll()

    @Query("select * from address WHERE Latitude BETWEEN :startLat AND :endLat " +
            "AND Longitude BETWEEN :startLng AND :endLng")
    fun queryRangeAddress(startLng: Float, endLng: Float, startLat: Float, endLat: Float):Flow<List<AddressInfo>>
}

@Entity(tableName = "address")
data class AddressInfo (
    @PrimaryKey
    var id: String,
    var latitude: Float,
    var longitude: Float,
    var addressName: String
)